package com.jiyun.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.dto.MaterialDto;
import com.jiyun.entity.Material;
import com.jiyun.entity.MaterialType;
import com.jiyun.mapper.MaterialMapper;
import com.jiyun.mapper.MaterialTypeMapper;
import com.jiyun.service.IMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-02-22
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements IMaterialService {

    @Autowired
    MaterialMapper materialMapper;

    @Autowired
    MaterialTypeMapper materialTypeMapper;
    @Override
    public Page<MaterialDto> findPage(Integer page, Integer pageSize) {
        Page<MaterialDto> materialDtoPage = new Page<>(page, pageSize);

        materialMapper.findPage(materialDtoPage);

        List<MaterialDto> records = materialDtoPage.getRecords();
        for (MaterialDto record : records) {
            String[] split = record.getTypeIds().split(",");

            String typeNames = "";
            for (String tid : split) {
                MaterialType materialType = materialTypeMapper.selectById(tid);
                typeNames = typeNames + materialType.getName() +"-";
            }
            typeNames = typeNames.substring(0,typeNames.length()-1);

            record.setTypeNames(typeNames);
        }



        return materialDtoPage;
    }
}
